﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.IO;

namespace ImageSpicer
{
    class Program
    {

       static string path = "";     //  path to image
       static Bitmap image;         //  image object in System.Drawing.Bitmap form
       static int count = 0;        //  how many times to save the image
       static int pixelCount = 0;   //  how many pixels to randomize

        static void Main(string[] args)
        {
            //check if there are any open parameters 
            if (args.Length > 0)
            {
                if (File.Exists(args[0]))
                    path = args[0];
                else
                    PrintCLIUse();

                if(args.Length > 1)
                    if (Int32.TryParse(args[1], out pixelCount) == false)
                        PrintCLIUse();

                if(args.Length > 2)
                    if (Int32.TryParse(args[2], out count) == false)
                        PrintCLIUse();
            }
               
            Loop();
        }

        static void Convert()
        {
            
            Random rand = new Random(DateTime.UtcNow.Millisecond);

            // start the randomization loop
            while (count > 0)
            {

                image = (Bitmap)Bitmap.FromFile(path);

                int tempCount = pixelCount;

                while (tempCount > 0)
                {
                    int x = rand.Next(0, (image.Width - 1));
                    int y = rand.Next(0, (image.Height - 1));
                    int r = rand.Next(255);
                    int g = rand.Next(255);
                    int b = rand.Next(255);
                    Color color = Color.FromArgb(r, g, b);

                    image.SetPixel(x, y, color);
                    tempCount--;
                }

                image.Save(Directory.GetParent(path).FullName + "\\" + count.ToString() + ".png");

                count--;
            }
        }

        static void Loop()
        {
            //handle image path
            while (!File.Exists(path))
            {
                Console.WriteLine("Enter the path to the image"
                    + "\n Example: X:\\herp\\derp\\nope.jpg");

                path = Console.ReadLine();
            }

            //handle amount of pixels to randomize
            while (pixelCount <= 0)
            {
                Console.WriteLine("Enter how many times you want the image to randomize");

                Int32.TryParse(Console.ReadLine(), out pixelCount);
            }

            //handle amount of images to save
            while (count <= 0)
            {
                Console.WriteLine("Enter how many times you want to spice the image");

                Int32.TryParse(Console.ReadLine(), out count);
            }

            Convert();
        }

        static void PrintCLIUse()
        {
            Console.WriteLine("Arguments for program: \n spicer.exe [path to image] {how many pixels to randomize} <how many times to repeat>");
        }
    }
}
